<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/11/25 0025
 * Time: 9:19
 */
namespace app\adminapi\controller;

use app\adminapi\model\Api;
use app\api\model\JWT;
use think\App;
use think\Controller;
use think\helper\Hash;
use think\Db;

/**
 * @title 首页
 * @description 接口说明
 * @header name:flag require:1 default: desc:手机设备号
 */
class Index extends Api
{
  /**
   * @title 宴会实时数据
   * @description 接口说明
   * @author 开发者
   * @url /adminapi/index/index
   * @method POST  
   * 
   * @return newArrs:列表@
   * @newArrs yesterday_order:昨日已定订单 yesterday_orders:昨日潜客订单 yesterday_scheduled:昨日押金
   * @return newArr:今日订单@
   * @newArr reserved:潜客订单 destine:已定订单 scheduled_money:今日押金总金额
   */
   public function index()
   {
    $user = $this->user;
    //当天开始时间
    $begin_time=strtotime(date("Y-m-d",time()));
     //当天结束之间
    $end_time=time();
    // 昨日时间
    $yesterday_begin_time = strtotime(date("Y-m-d",strtotime("-1 day")));
    $yesterday_end_time = strtotime(date("Y-m-d",strtotime("-1 day")))+86399;
    $where[] = ['hid','=',$user['hid']];
    $where[] = ['addtime','between',[$begin_time,$end_time]];
    $scheduled_money =  db::name('scheduled_deposit')->where($where)->where('status',2)->sum('money');

    $where[] = ['is_del','=',2];
    $scheduled =  db::name('scheduled')->where($where)->field('id,type')->select();
    $datas = $this->dataGroup($scheduled,'type');
    foreach ($datas as $k => $v) {
      // 预定
      if($k == 2){
        $newArr['destine'] = count($v);
      }
      // 潜客
      if($k == 1){
        $newArr['reserved'] = count($v);
      }
    }
    if(!isset($newArr['destine'])){
      $newArr['destine'] = 0;
    }
    if(!isset( $newArr['reserved'])){
      $newArr['reserved'] = 0;
    }
    $newArr['scheduled_money'] = $scheduled_money;


    $wheres[] = ['hid','=',$user['hid']];
    $yesterday_order = db::name('scheduled')->where($wheres)->where('type',2)->count();
    $yesterday_orders = db::name('scheduled')->where($wheres)->where('type',1)->count();

    $wheres[] = ['addtime','between',[$yesterday_begin_time,$yesterday_end_time]];
    $yesterday_scheduled =  db::name('scheduled_deposit')->where($wheres)->where('status',2)->sum('money');
    $newArrs['yesterday_order'] = $yesterday_order;
    $newArrs['yesterday_orders'] = $yesterday_orders;
    $newArrs['yesterday_scheduled'] = $yesterday_scheduled;
    $data['newArrs'] = $newArrs;
    $data['newArr'] = $newArr;
    return $this->ajaxReturn(0,'成功',$data);
   }
   /**
     * @title 宴会累计数据
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/index/grand_total
     * @method POST  
     * 
     * @return scheduled:累计订单
     * @return client:累计客户
     * @return scheduled_deposit:累计押金
     * @return check:累计收入
     */
   public function grand_total()
   {
      $user = $this->user;
      $where[] = ['hid','=',$user['hid']];
      // 累计订单
      $scheduled =  db::name('scheduled')->where($where)->where('is_del',2)->count();
      // 累计客户
      $client = db::name('client')->where($where)->count();
      // 累计押金
      $scheduled_deposit = db::name('scheduled_deposit')->where($where)->where('status',2)->sum('money');
      // 累计收入
      $check = db::name('check_order')->where($where)->sum('total_money');

      $data['scheduled'] = $scheduled;
      $data['client'] = $client;
      $data['scheduled_deposit'] = $scheduled_deposit;
      $data['check'] = $check;

      return $this->ajaxReturn(0,'成功',$data);
   }
   protected function dataGroup(array $dataArr,string $keyStr)
    {
        $newArr=[];
        foreach ($dataArr as $k => $val) {    //数据根据日期分组
            $newArr[$val[$keyStr]][] = $val;
        }
        return $newArr;
    }
     /**
     * @title 点击量
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/index/save_icon
     * @method POST  
     * @param name:alias type:string require:1 default: other: desc:alias值
     * 
     */
    public function save_icon()
    {
        $alias = input('alias');
        if(!$alias){
          return $this->ajaxReturn(1,'缺少参数');
        }
        db::name('icon')->where('alias',$alias)->setInc('num',1);
        return $this->ajaxReturn(0,'成功');
    }
     /**
     * @title 点击率排序
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/index/get_icon
     * @method POST  
     * 
     * @return icon:列表@
     * @icon id:id name:名称 alias:别名 icon:图标  details:详细描述
     * 
     */
    public function get_icon()
    {
      $res = db::name('icon')->field('id,name,alias,icon,num,details')->order('num desc')->limit(3)->select();
      if($res){
        foreach($res as $k=>$v){
          if($v['icon']){
            $res[$k]['icon'] = get_file_path($v['icon']);
          }
        }
      }
      $data['icon'] = $res;
      return $this->ajaxReturn(0,'成功',$data);
    }

}